﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using EnnErrE.BillFiller;

namespace nr.GLV.Windows
{
    public partial class BillSettingsForm : Form
    {
        public enum BillAlignment { Standard = 0, A4Left = 1, A4Right = 2 }
        public string Company { get { return txtCompany.Text; } set { txtCompany.Text = value; } }
        public string Printer { get { return cbPrinter.SelectedPrinter; } set { cbPrinter.SelectedPrinter = value; } }
        public string Motivation { get { return txtMotivation.Text; } set { txtMotivation.Text = value; } }
        public string AccountNumber { get { return txtAccountNumber.Text; } set { txtAccountNumber.Text = value; } }
        public bool Landscape
        {
            get { return cbOrientation.SelectedIndex == 1; }
            set { cbOrientation.SelectedIndex = value ? 1 : 0; }
        }
        public BillSettings Settings { get { return cbBillType.SelectedItem as BillSettings; } set { cbBillType.SelectedItem = value; } }
        public BillAlignment Alignment { get { return (BillAlignment)cbAlignment.SelectedIndex; } set { cbAlignment.SelectedIndex = (int)value; } }

        public BillSettingsForm()
        {
            InitializeComponent();
        }

        private void BillSettingsForm_Load(object sender, EventArgs e)
        {
            settings.FileName = string.Format("{0}_{1}.settings", Application.ProductName, Application.ProductVersion);
            Motivation =
                (settings["causale"].Length == 0) ? string.Format("Canone Lampade Votive - Anno {0:yyyy}", DateTime.Now) : settings["causale"];
            AccountNumber = settings["numerocc"];
            Company = settings["titolarecc"];
            Landscape = false;
            Alignment = BillAlignment.Standard;
            cbBillType.Items.Add(BillSettings.Mod12401A);
            cbBillType.Items.Add(BillSettings.Mod12409A);
            cbBillType.SelectedIndex = 0;
        }

        private void TextBoxContentChanged(object sender, EventArgs e)
        {
            EnableButtons();
        }

        private void txtAccountNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar));
        }

        private void EnableButtons()
        {
            btnOk.Enabled = (txtAccountNumber.Text.Length > 0) && (txtCompany.Text.Length > 0) && (txtCompany.Text.Length > 0);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            settings["causale"] = Motivation;
            settings["numerocc"] = AccountNumber;
            settings["titolarecc"] = Company;
            settings.Save();
        }
    }
}
